package com.sdleyou.treepro.common.service.impl;

import com.sdleyou.treepro.common.exception.ServiceException;
import com.sdleyou.treepro.common.model.convert.PropDO2PropVOConverter;
import com.sdleyou.treepro.common.model.dao.PropDO;
import com.sdleyou.treepro.common.model.dto.PropDTO;
import com.sdleyou.treepro.common.model.enums.ErrorCodeEnum;
import com.sdleyou.treepro.common.model.utils.RandomUtil;
import com.sdleyou.treepro.common.model.vo.PropVO;
import com.sdleyou.treepro.common.service.MongoPageManager;
import com.sdleyou.treepro.common.service.PropManager;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 道具管理实现类
 */
@Service
public class PropManagerImpl implements PropManager {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private MongoPageManager mongoPageManager;

    @Override
    public void createProp(PropDTO propDTO) {
        PropDO propDO = new PropDO();
        propDO.setPropId(RandomUtil.randomId());
        propDO.setPropName(propDTO.getPropName());
        propDO.setPropDesc(propDTO.getPropDesc());
        propDO.setPropIcon(propDTO.getPropIcon());
        propDO.setPropImage(propDO.getPropImage());
        propDO.setPropType(propDO.getPropType());
        propDO.setPropValue(propDO.getPropValue());
        propDO.setPropEffect(propDO.getPropEffect());
        Date date = new Date();
        propDO.setGmtCreate(date.getTime());
        propDO.setGmtModified(date.getTime());
        mongoTemplate.insert(propDO);
        return;
    }

    @Override
    public void updateProp(PropDTO propDTO, String propId) {
        Query query = new Query(Criteria.where("prop_id").is(propId));
        PropDO propDO = mongoTemplate.findOne(query, PropDO.class);
        if (propDO == null) {
            throw new ServiceException(ErrorCodeEnum.E005.code(), "道具不存在");
        }
        Update update = new Update();
        if (!StringUtils.isEmpty(propDTO.getPropName())) {
            update.set("prop_name", propDTO.getPropName());
        }
        if (!StringUtils.isEmpty(propDTO.getPropDesc())) {
            update.set("prop_desc", propDTO.getPropDesc());
        }
        if (!StringUtils.isEmpty(propDTO.getPropDesc())) {
            update.set("prop_desc", propDTO.getPropDesc());
        }
        if (!StringUtils.isEmpty(propDTO.getPropIcon())) {
            update.set("prop_icon", propDTO.getPropIcon());
        }
        if (!StringUtils.isEmpty(propDTO.getPropImage())) {
            update.set("prop_image", propDTO.getPropImage());
        }
        if (!StringUtils.isEmpty(propDTO.getPropEffect())) {
            update.set("prop_effect", propDTO.getPropEffect());
        }
        mongoTemplate.updateFirst(query, update, PropDO.class);
        return;
    }

    @Override
    public PropVO findOne(String propType) {
        Query query = new Query(Criteria.where("prop_type").is(propType));
        PropDO propDO = mongoTemplate.findOne(query, PropDO.class);
        PropVO propVO = new PropVO();
        if(propDO!=null) {
            BeanUtils.copyProperties(propDO, propVO);
        }
        return propVO;
    }

    /**
     * 获取道具信息列表
     *
     * @return
     */
    @Override
    public List<PropVO> propList() {
        Query query = new Query();
        List<PropDO> treeDOList = mongoTemplate.find(query, PropDO.class);
        return treeDOList.stream().map(PropDO2PropVOConverter::convert).collect(Collectors.toList());
    }
}
